<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微博首页</title>
</head>
<body>
  <h1>微博首页</h1>
  <div>
      <div v-show="user==''"><!--user没有值代表未登录-->
          <a href="/reg.html">注册</a>
          <a href="/login.html">登录</a>
      </div>
    <div v-show="user!=''"><!--user有值代表登录过-->
        <p>{{user.nickname}}，欢迎回来</p>
        <a href="/send.html">发布微博</a>
        <a href="/me.html">我的微博</a>
        <a href="javascript:void(0)" @click="logout()">登出</a>
    </div>
    <div v-for="weibo in arr">
        <h3>
            <a :href="'/detail.html?id='+weibo.id">
            {{weibo.author}}说:{{weibo.content}}
        </a>
        </h3>
        <p>发布于:{{weibo.created}}</p>
        <a :href="'/detail.html?id='+weibo.id">
            <img :src="weibo.url" style="width: 200px" alt="">
        </a>
        <hr>
    </div>
  </div>
<!--引入axios和vue-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="https://cdn.staticfile.org/moment.js/2.24.0/moment.min.js"></script>
  <script>
     let v = new Vue({
         el:"body>div",
         data:{
             user:"",//当前登录的用户对象
             arr:[] //装微博的数组
         },
         created:function (){
             //发起请求得到当前登录的用户信息
             axios.get("/currentUser").then(function (res) {
                 v.user=res.data;
                 console.log("user="+v.user);
             })
             //发起请求得到所有用户的微博数据
             axios.get("/weibo/select").then(function (res) {
                 v.arr = res.data;
                 //遍历微博数组，修改每个微博对象的created属性
                 for(let weibo of v.arr){
                     weibo.created
                         = moment(weibo.created).format("YYYY年MM月DD日 HH:mm:ss");
                 }
             })
         },
         methods:{
             logout:function () {
                 if(confirm("您确认要退出登录吗?")){
                     axios.get("/logout").then(function () {
                         location.reload();//刷新页面
                     })
                 }
             }
         }
     })
  </script>
</body>
</html>